package com.sto.stoapp.activity.scanner.business;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;

import com.sto.stoapp.StoApplication;
import com.sto.stoapp.activity.R;
import com.sto.stoapp.adapter.SelectTableAdapter;
import com.sto.stoapp.dataprocess.parcelable.GetTableParcel;
import com.sto.stoapp.dataprocess.parcelable.SelectTableParcel;
import com.sto.stoapp.db.StoDataBaseOperation;
import com.sto.stoapp.db.manager.SelectTableManager;
import com.sto.stoapp.db.manager.SqlSelectTableManager;
import com.sto.stoapp.http.MessageType;
import com.sto.stoapp.server.StoDownServer;
import com.sto.stoapp.thread.BaseThread;
import com.sto.stoapp.util.Helper;
import com.sto.stoapp.widget.BaseActivity;

import java.util.List;

import sto.android.app.StoPdaKeyEvent;

public class BusinessListActivity extends BaseActivity {

    private ListView updateList;
    // private TextView title;
    private Button backButton, updateButton, displayCount;

    private SelectTableAdapter courierAdapter;
    private List<GetTableParcel> courierDatas;
    private SelectTableParcel Entity;

    private String titleName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.sto_business_list);
        initWidget();
        initListener();
    }

    private void initWidget() {
        // title = (TextView) findViewById(R.id.title);
        updateList = (ListView) findViewById(R.id.update_listview);
        updateButton = (Button) findViewById(R.id.list_update);
        backButton = (Button) findViewById(R.id.return_back);
        displayCount = (Button) findViewById(R.id.display_count);
        displayCount.setFocusable(false);
        Entity = this.getIntent().getParcelableExtra("businessListData");
        titleName = Entity.getProperty();
        // title.setText(titleName);
        setStoTitle(titleName);
        Entity.setProperty(null);
        BufferThread bufferThread = new BufferThread(this);
        bufferThread.start();

    }

    private void initListener() {
        updateButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                updateListData();

            }
        });

        backButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    // 更新列表数据
    private void updateListData() {
        if (Helper.isNetworkAvailable(this)) {
            Helper.showToast(BusinessListActivity.this,
                    getString(R.string.upgrade_data_hint));
            // 网点列表
            if (titleName.equals(getString(R.string.site_full_list))) {
                StoDataBaseOperation
                        .getInstance(BusinessListActivity.this)
                        .clearTableData(SqlSelectTableManager.SQL_NETSITE_CLEAR);

                Intent downIntent = new Intent(StoDownServer.UPDATE_ACTION);
                downIntent.putExtra(StoDownServer.SERVER_DOWN_STATUS,
                        new int[]{MessageType.SITE_CODE_FULL});
                sendBroadcast(downIntent);
            }
            // 员工列表
            else if (titleName.equals(getString(R.string.sales_list))) {
                StoDataBaseOperation.getInstance(BusinessListActivity.this)
                        .clearTableData(SqlSelectTableManager.SQL_STAFF_CLEAR);

                Intent downIntent = new Intent(StoDownServer.UPDATE_ACTION);
                downIntent.putExtra(StoDownServer.SERVER_DOWN_STATUS,
                        new int[]{MessageType.SITE_SALEAS_TYPE});
                sendBroadcast(downIntent);
            }
            // 问题件原因表
            else if (titleName.equals(getString(R.string.problem_list))) {
                StoDataBaseOperation
                        .getInstance(BusinessListActivity.this)
                        .clearTableData(SqlSelectTableManager.SQL_PROBLEM_CLEAR);

                Intent downIntent = new Intent(StoDownServer.UPDATE_ACTION);
                downIntent.putExtra(StoDownServer.SERVER_DOWN_STATUS,
                        new int[]{MessageType.PROBLEM_TYPE});
                sendBroadcast(downIntent);
            }
            // 下一站网点资料
            else if (titleName.equals(getString(R.string.next_site_list))) {
                StoDataBaseOperation.getInstance(BusinessListActivity.this)
                        .clearTableData(
                                SqlSelectTableManager.SQL_NEXT_NETSITE_CLEAR);

                Intent downIntent = new Intent(StoDownServer.UPDATE_ACTION);
                downIntent.putExtra(StoDownServer.SERVER_DOWN_STATUS,
                        new int[]{MessageType.NEXT_SITE_CODE});
                sendBroadcast(downIntent);
            }
            // 网点省份列表
            else if (titleName.equals(getString(R.string.province_list))) {
                StoDataBaseOperation.getInstance(BusinessListActivity.this)
                        .clearTableData(
                                SqlSelectTableManager.SQL_PROVINCE_CLEAR);

                Intent downIntent = new Intent(StoDownServer.UPDATE_ACTION);
                downIntent.putExtra(StoDownServer.SERVER_DOWN_STATUS,
                        new int[]{MessageType.PROVINCE_TYPE});
                sendBroadcast(downIntent);
            }
            // 服务短信列表
            else if (titleName.equals(getString(R.string.service_SMS_list))) {

                Intent downIntent = new Intent(StoDownServer.UPDATE_ACTION);
                downIntent.putExtra(StoDownServer.SERVER_DOWN_STATUS,
                        new int[]{MessageType.SERVER_SMS_TYPE});
                sendBroadcast(downIntent);

            }
            // 订单打回原因
            else if (titleName.equals(getString(R.string.order_back_reason))) {
                StoDataBaseOperation.getInstance(BusinessListActivity.this)
                        .clearTableData(
                                SqlSelectTableManager.SQL_ORDER_REASON_CLEAR);

                Intent downIntent = new Intent(StoDownServer.UPDATE_ACTION);
                downIntent.putExtra(StoDownServer.SERVER_DOWN_STATUS,
                        new int[]{MessageType.ORDER_REASON_TYPE});
                sendBroadcast(downIntent);
            }
            // 路由表
            else if (titleName.equals(getString(R.string.route_list))) {
                StoDataBaseOperation.getInstance(BusinessListActivity.this)
                        .clearTableData(SqlSelectTableManager.SQL_ROUTE_CLEAR);
                Intent downIntent = new Intent(StoDownServer.UPDATE_ACTION);
                downIntent.putExtra(StoDownServer.SERVER_DOWN_STATUS,
                        new int[]{MessageType.AVIATION_ROUTE});
                sendBroadcast(downIntent);
            }

            finish();
        } else {
            Helper.showToast(BusinessListActivity.this,
                    getString(R.string.network_failure));
        }
    }

    /**
     * 显示数据缓冲线程
     *
     * @author Administrator
     */
    class BufferThread extends BaseThread {

        private Context context;

        public BufferThread(Context context) {
            super(context);
            this.context = context;
        }

        @Override
        public void run() {
            SelectTableManager selectTable = new SelectTableManager(context);
            courierDatas = selectTable.queryAll(Entity);
            final int count = courierDatas == null ? 0 : courierDatas.size();

            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    courierAdapter = new SelectTableAdapter(context,
                            courierDatas, Entity);
                    updateList.setAdapter(courierAdapter);
                    displayCount.setText("" + count);
                    if (count > 0) {
                        updateList.requestFocus();
                    }
                }
            });
        }
    }

    // 监听按键
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        keyCode = StoApplication.getStoJni().getEventFuction(keyCode);
        switch (keyCode) {

            case StoPdaKeyEvent.KEYCODE_MENU:
                updateListData();
                Helper.showToast(BusinessListActivity.this,
                        getString(R.string.upgrade_data_hint));
                finish();
                break;
            default:
                break;
        }
        return super.onKeyUp(keyCode, event);
    }
}
